package com.ygqh.baby.service.impl;

import com.ygqh.baby.ao.*;
import com.ygqh.baby.model.ngmm.NgmmOrder;
import com.ygqh.baby.model.ngmm.NgmmResult;
import com.ygqh.baby.po.*;
import com.ygqh.baby.service.*;
import com.ygqh.baby.service.ngmm.api.NgmmApi;
import com.ygqh.baby.service.ngmm.util.NgmmConfigure;
import com.ygqh.baby.utils.ExceptionUtil;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

@Service
public class YgNgmmServiceImpl implements YgNgmmService {
	protected final Logger logger = Logger.getLogger(this.getClass());
	private static final ExecutorService threadPool = Executors.newFixedThreadPool(5);

	@Autowired
	private NgmmApi ngmmApi;
	@Autowired
	private YgSkuService ygSkuService;
	@Autowired
	private YgProductService ygProductService;
	@Autowired
	private YgProductImagesService ygProductImagesService;
	@Autowired
	private YgStockService ygStockService;
	@Autowired
	private YgThirdPartyService ygThirdPartyService;
	@Autowired
	private YgOrderLogService ygOrderLogService;

	@Override
	public void saveNgmmOrders(List<NgmmOrder> orders) {
		if (CollectionUtils.isEmpty(orders)) {
			return;
		}
		List<YgOrder> ygOrders = new ArrayList<>();
		List<YgStock> stocks = new ArrayList<YgStock>();
		for (YgOrder order : orders) {
			order.setOrderStatus(OrderStatus.WaitingSend);
			order.setOrderType(OrderType.Ordinary);
			order.setPayType(PayType.WeChat);
			order.setSourceCode(NgmmConfigure.SOURCE_CODE);
			order.setUserId(NgmmConfigure.DEFAULT_USER_ID);
			order.setStatus(DataStatus.Valid);
			order.setCommentStatus(CommentStatus.UnComplete);
			order.setSupplierId(NgmmConfigure.DEFAULT_SUPPLIER_ID);
			order.setPaidPrice(order.getTotalPrice());// 支付金额
			order.setCreateTime(new Date());
			order.setPaidTime(new Date());
			order.setUserRemark("saveNgmmOrders");
			order.setPlatformNo("M003");
			order.setPlatformName("年糕妈妈");

			List<YgOrderDetail> detailList = new ArrayList<YgOrderDetail>();
			BigDecimal productPrice = new BigDecimal("0.00");
			for (YgOrderDetail detail : order.getOrderDetail()) {
				YgSku sku = ygSkuService.findBySkuCode(detail.getSkuCode(), PromotionType.Ordinary);
				YgProduct product = ygProductService.findById(sku.getProductId());
				YgProductImages images = ygProductImagesService.findMainImgByProductId(product.getId());
				detail.setOrderId(order.getOrderId());
				detail.setProductName(product.getProductName());
				detail.setPromotionType(PromotionType.Ordinary);
				detail.setSize(sku.getSizeName());
				detail.setSkuCode(sku.getSkuCode());
				detail.setSkuId(sku.getId());
				detail.setUnitName(product.getUnitName());
				detail.setImageUrl(images.getImagePath());
				detail.setShortName(product.getShortName());
				detail.setSupplierId(NgmmConfigure.DEFAULT_SUPPLIER_ID);
				detailList.add(detail);

				YgStock stock = new YgStock();
				stock.setSkuCode(sku.getSkuCode());
				stock.setAmount(detail.getQuantity());
				stocks.add(stock);
				
				productPrice = productPrice.add(detail.getSalePrice().multiply(BigDecimal.valueOf(detail.getQuantity())));
			}
			order.setProductPrice(productPrice);// 商品总额
			order.setOrderDetail(detailList);
			ygOrders.add(order);
		}
		ygThirdPartyService.saveYgOrderList("035", ygOrders);
		ygStockService.reduceStock(stocks, "saveNgmmOrders");
	}
	
	@Override
	public void updateNgmmExpress(String orderNo, String expressCode, String expressNo) {
		threadPool.execute(new Runnable() {

			@Override
			public void run() {
				try {
					String code = expressCode;
					switch (expressCode) {
					case "YD":
						code = "YUNDA";
						break;
					case "HHTT":
						code = "TTKDEX";
						break;
					case "DBL":
						code = "DBWL";
						break;
					case "ANE":
						code = "ANWL";
						break;
					case "YCWL":
						code = "YCKD";
						break;
					case "LHT":
						code = "YCKD";
						break;
					case "STWL":
						code = "STKY";
						break;
					default:
						break;
					}
					NgmmResult result = ngmmApi.orderUpdateExpress(orderNo, code, expressNo);
					ygOrderLogService.addOrderLog(Long.valueOf(orderNo), OrderStatus.WaitingSign, NgmmConfigure.SOURCE_CODE,
							"年糕妈妈订单快递信息修改成功。;orderId=" + orderNo + ";result:" + result.toString());
				} catch (Exception e) {
					ygOrderLogService.addOrderLog(Long.valueOf(orderNo), OrderStatus.WaitingSign, NgmmConfigure.SOURCE_CODE,
							"年糕妈妈订单快递信息修改失败。;orderNo=" + orderNo + ";" + ExceptionUtil.getExceptionMsg(e));
					e.printStackTrace();
				}
			}
		});
	}

}
